package com.atguigu.gmallre.mock.stage;


import com.atguigu.gmallre.mock.bean.*;
import com.atguigu.gmallre.mock.config.AppConfig;
import com.atguigu.gmallre.mock.constant.GmallConstant;
import com.atguigu.gmallre.mock.factory.UserSession;
import com.atguigu.gmallre.mock.log.AppCommon;
import com.atguigu.gmallre.mock.log.AppDisplay;
import com.atguigu.gmallre.mock.log.AppMain;
import com.atguigu.gmallre.mock.log.AppPage;
import com.atguigu.gmallre.mock.log.enums.PageId;
import com.atguigu.gmallre.mock.service.*;
import com.atguigu.mock.util.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


@Component("orderList")
public class OrderListStage implements Stage {

    @Autowired
    OrderInfoService orderInfoService;

    @Autowired
    OrderRefundInfoService orderRefundInfoService;

    @Autowired
    RefundPaymentService refundPaymentService;

    @Autowired
    CommentInfoService commentInfoService;

    @Autowired
    OrderStatusLogService orderStatusLogService;

    @Autowired
    LogService logService;

    @Autowired
    LoginStage loginStage;

    @Override
    public boolean handle() {


        AppMain appMain= mainLog();
        loginStage.checkLogin(appMain);

        handleOrder(  appMain);

        logService.doLog(appMain) ;
        UserSession.setLastPageId(PageId.orders_all);
        return true;
    }



    private AppMain mainLog(){
        AppCommon appCommon = UserSession.getAppCommon();

        Integer durTime= RandomNum.getRandInt(5000, AppConfig.page_during_max_ms);

        AppPage appPage = AppPage.builder()
                .last_page_id(UserSession.getLastPageId())
                .during_time(durTime)
                .page_id(PageId.orders_all).buildFinal();



        UserSession.addTimeByDuringTime(appPage.getDuring_time());
        Long ts= UserSession.getCurDateTime().getTime();
        AppMain appMain = AppMain.builder().common(appCommon).page(appPage) .ts(ts).build();
        return appMain;
    }


    public void handleOrder(AppMain appMain){
        Long userId = UserSession.getUserInfo().getId();
        List<OrderInfo> orderInfoList = orderInfoService.listWithDetail(new QueryWrapper<OrderInfo>().eq("user_id", userId).eq("order_status", GmallConstant.ORDER_STATUS_PAID),true);

        Boolean ifRefund=  RandomBox.builder().add(true ,AppConfig.if_refund_rate)
                .add(false,100-AppConfig.if_refund_rate).build().getRandBoolValue();

        if(ifRefund && orderInfoList.size()>0){
            OrderInfo orderInfo = RandomCollection.getOneFrom(orderInfoList);
            orderInfo.setOrderStatus(GmallConstant.ORDER_STATUS_REFUNDING);
            orderInfo.setOperateTime(UserSession.getCurDateTime());
            orderInfoService.saveOrUpdate(orderInfo,true);
            orderStatusLogService.genOrderStatusLog(orderInfo);


            OrderRefundInfo orderRefundInfo = genRefundOrderInfo(orderInfo);
            orderRefundInfoService.saveOrUpdate(orderRefundInfo);

        }else if(orderInfoList.size()>0){
            OrderInfo orderInfo = RandomCollection.getOneFrom(orderInfoList);
            orderInfo.setOrderStatus(GmallConstant.ORDER_STATUS_FINISH);
            orderInfo.setOperateTime(UserSession.getCurDateTime());
            List<CommentInfo> commentInfoList = genCommentInfo(orderInfo);
            commentInfoService.saveOrUpdateBatch(commentInfoList ,1000,true);

        }




    }


    public OrderRefundInfo genRefundOrderInfo(OrderInfo orderInfo){

        RandomBox<String> refundTypeOptionGroup=new RandomBox(new RanOpt(GmallConstant.REFUND_TYPE_ONLY_MONEY,30),new RanOpt(GmallConstant.REFUND_TYPE_WITH_GOODS,60) );

        RandomBox<String> refundReasonOptionGroup=new RandomBox(new RanOpt(GmallConstant.REFUND_REASON_BAD_GOODS,30),
                new RanOpt(GmallConstant.REFUND_REASON_SIZE_ISSUE,10),
                new RanOpt(GmallConstant.REFUND_REASON_SALE_OUT,10),
                new RanOpt(GmallConstant.REFUND_REASON_MISTAKE,11),
                new RanOpt(GmallConstant.REFUND_REASON_WRONG_DESC,12),
                new RanOpt(GmallConstant.REFUND_REASON_NO_REASON,16),
                new RanOpt(GmallConstant.REFUND_REASON_OTHER,8)
        );

        OrderRefundInfo orderRefundInfo = new OrderRefundInfo();
        orderRefundInfo.setOrderId(orderInfo.getId());
        OrderDetail orderDetail = orderInfo.getOrderDetailList().get(0);
        orderRefundInfo.setRefundAmount(  orderDetail.getOrderPrice().multiply(BigDecimal.valueOf(orderDetail.getSkuNum()) ) );
        orderRefundInfo.setSkuId(orderDetail.getSkuId());
        orderRefundInfo.setUserId(orderInfo.getUserId());
        orderRefundInfo.setRefundStatus(GmallConstant.REFUND_STATUS_APPROVING);
        orderRefundInfo.setRefundNum(orderDetail.getSkuNum());
        orderRefundInfo.setCreateTime(UserSession.getCurDateTime());
        orderRefundInfo.setRefundReasonTxt("退款原因具体："+ RandomNumString.getRandNumString(0,9,10,""));
        orderRefundInfo.setRefundType(refundTypeOptionGroup.getRandStringValue());
        orderRefundInfo.setRefundReasonType(refundReasonOptionGroup.getRandStringValue());
        return orderRefundInfo;
    }





         private   List<CommentInfo> genCommentInfo( OrderInfo orderInfo  ) {


             RandomBox<String> appraiseOptionGroup = new RandomBox(new RanOpt(GmallConstant.APPRAISE_GOOD, AppConfig.appraise_weight[0]),
                     new RanOpt(GmallConstant.APPRAISE_SOSO, AppConfig.appraise_weight[1]),
                     new RanOpt(GmallConstant.APPRAISE_BAD, AppConfig.appraise_weight[2])
                     , new RanOpt(GmallConstant.APPRAISE_AUTO, AppConfig.appraise_weight[3]));

             List<CommentInfo> commentInfoList=new ArrayList<>();
             for (OrderDetail orderDetail : orderInfo.getOrderDetailList()) {
                 SkuInfo skuInfo = orderDetail.getSkuInfo();
                 CommentInfo commentInfo = new CommentInfo();
                 commentInfo.setOrderId(orderInfo.getId());
                 commentInfo.setSkuId(skuInfo.getId());
                 commentInfo.setSpuId(skuInfo.getSpuId());
                 commentInfo.setUserId(UserSession.getUserInfo().getId());
                 commentInfo.setNickName(UserSession.getUserInfo().getNickName());
                 commentInfo.setHeadImg(UserSession.getUserInfo().getHeadImg());
                 commentInfo.setCommentTxt("评论内容：" + RandomNumString.getRandNumString(1, 9, 50, ""));
                 commentInfo.setCreateTime(UserSession.getCurDateTime());
                 commentInfo.setAppraise(appraiseOptionGroup.getRandStringValue());
                 commentInfoList.add(commentInfo);
             }
            return  commentInfoList;
         }



}
